package com.tvunetworks.center.user.controller;

import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.user.annotation.Logined;
import com.tvunetworks.center.user.model.vo.MenuListVo;
import com.tvunetworks.center.user.service.MenuService;
import com.tvunetworks.center.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @program: tvucc-user
 * @description: MenuController
 * @author lebronchen
 * @create: 2019-03-19 19:04
 **/
@RestController
@RequestMapping("/menu")
public class MenuController {
    private static final Logger LOGGER = LoggerFactory.getLogger(MenuController.class);
    @Autowired
    private MenuService menuService;

    @Autowired
    private UserService userService;

    /**
     *
     * @return Result
     */
    @GetMapping("/list")
    public Result<Object> getMenuList(){
        MenuListVo vo = menuService.getMenuList();
        return ResultUtil.success(vo);
    }

    /**
     * 查询公共menu的信息
     * @param  session session
     * @return Result Result
     */
    @GetMapping("/menuInfoList")
    public Result<Object> getMenuInfoList(String session){
        MenuListVo vo = null;
        LoginUser user=null;
        try {
            Result<LoginUser> result = getLoginUser(session);
            if(result !=null){
                user = result.getResult();
            }
            if (user == null) {
                return ResultUtil.noLogin();
            }
            vo = menuService.getMenuInfoList(user.getId(),user.getRole());
        } catch (Exception e) {
            LOGGER.error("menuInfoList Exception",e);
        }
        return ResultUtil.success(vo);
    }
    /**
     * 查询公共menu的信息
     * @return Result
     * @param  user user
     */
    @PostMapping("/menuInfoListCookie")
    public Result<Object> menuInfoListCookie(@Logined LoginUser user){
        if (user == null) {
            return ResultUtil.noLogin();
        }
        MenuListVo vo = null;
        try {
            vo = menuService.getMenuInfoList(user.getId(),user.getRole());
        } catch (Exception e) {

        }
        return ResultUtil.success(vo);
    }

    /**
     *
     * @param session session
     * @return Result
     * @throws Exception 编译异常
     */
    public Result<LoginUser> getLoginUser(String session) throws Exception{
            return userService.getLoginUser(session);
    }
}
